package com.org.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.org.common.PageBean;
import com.org.mapper.BannerMapper;
import com.org.pojo.Banner;
import com.org.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: BannerServiceImpl
 * Package: com.org.service.impl
 * Description:
 *
 * @Author Origin
 * @Create 2024/2/24 22:01
 * @Version 1.0
 */

@Service
public class BannerServiceImpl implements BannerService {
    @Autowired
    private BannerMapper bannerMapper;
    @Override
    public void add(Banner banner) {
        bannerMapper.insert(banner);
    }


    @Override
    public void delete(Integer id) {
        bannerMapper.deleteById(id);
    }

    @Override
    public Banner detail(Integer id) {
        Banner b = bannerMapper.selectById(id);
        return b;
    }

    @Override
    public void update(Banner banner) {
        bannerMapper.updateById(banner);
    }

    @Override
    public List<Banner> list() {
        List<Banner> l = bannerMapper.list();
        return l;
    }

    @Override
    public PageBean<Banner> allList(Integer pageNum, Integer pageSize, String title, String content, String communityId) {
        //1. 创建PageBean对象
        PageBean<Banner> pb = new PageBean<>();

        //2.开启分页查询
        PageHelper.startPage(pageNum, pageSize);

        //3.调用mapper
        QueryWrapper<Banner> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", title)
                .like("content", content);

        if(communityId!=null&&!communityId.equals("")){
            queryWrapper.eq("community_id", communityId);
        }

        List<Banner> as = bannerMapper.selectList(queryWrapper);

        //Page中提供了方法，可以获取PageHelper分页查询后 得到的总记录条数和当页数据
        Page<Banner> p = (Page<Banner>) as;

        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());  //总共多少页
        pb.setItems(p.getResult()); //当前页内容

        return pb;
    }


}
